package com.lanxin.base;

import com.lanxin.util.PageBean;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;

@Repository
public class BaseDaoImpl implements BaseDao {

    @Autowired
    HibernateTemplate HibernateTemplate;

    public HibernateTemplate getHibernateTemplate() {

        return HibernateTemplate;

    }

    public void add(Object o) {//添加
        HibernateTemplate.save(o);

    }

    public void update(Object o) {//修改
        HibernateTemplate.update(o);
    }

    public void delete(Object o) {//删除
        HibernateTemplate.delete(o);
    }

    public Object getById(Class c, Integer id) {//根据id查询

        return HibernateTemplate.get(c, id);

    }

    public List queryAll(String hql) {//hql查询
        return HibernateTemplate.find(hql);
    }

    @SuppressWarnings("unchecked")
    public Object selectByHQL(final String hql) {//hql单条查询
        return HibernateTemplate.execute(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                Query q = session.createQuery(hql);
                return q.uniqueResult();
            }
        });
    }


    public void deletebysql(String sql) {
        Session session = HibernateTemplate.getSessionFactory().openSession();
        Transaction ts = session.beginTransaction();
        Query query = session.createSQLQuery(sql);
        query.executeUpdate();
        ts.commit();
        session.close();
        ;
    }

    public <T> PageBean<T> pageFenYE(final String hql, final Integer page,final Integer pageSize) {
        List<T> list = (List<T>) pageSelectAssist(hql,page,pageSize);
        int rowCount = rowCountSelect(hql);
        if(list!=null && list.size()>0){
            //计算总页数
            int pageCount = rowCount%pageSize==0?rowCount/pageSize:rowCount/pageSize+1;
            PageBean<T> pageBean = new PageBean<>(page,pageSize,rowCount,pageCount,list);
            return pageBean;
        }
        return null;
    }

    @Transactional(readOnly = false)
    @Override
    public void saveOrUpdate(Object o) {
        HibernateTemplate.saveOrUpdate(o);
    }

    //分页辅助(查询当前页数据)
    private <T> List<T> pageSelectAssist(String hql,Integer page,Integer pageSize){
        List<T> list = (List<T>)HibernateTemplate.execute(new HibernateCallback<Object>() {
            @Override
            public Object doInHibernate(Session session) throws HibernateException {
                Query query = session.createQuery(hql);
                query.setFirstResult((page-1)*pageSize);
                query.setMaxResults(pageSize);
                return query.list();
            }
        });
        return list;
    }

    //分页辅助(获取分页查询所有条数)
    private <T> int rowCountSelect(String hql){
        List<T> list = (List<T>) HibernateTemplate.find(hql);
        if(list!=null && list.size()>0){
            return list.size();
        }
        return 0;
    }
}
